window.onload = function () {
    /**
     * 242. 有效的字母异位词
     */
    const s = "anagram"
    const t = "nagaram"
    console.log(isAnagram(s, t))

    /**
     * 1. 两数之和
     */
    const nums = [2, 7, 11, 15]
    const target = 9
    console.log(twoSum(nums, target))

    /**
     * 49. 丑数
     */
    const n = 10
    console.log(nthUglyNumber(n))
}

/**
 * 242. 有效的字母异位词
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isAnagram = function (s, t) {
    return s.length == t.length && [...s].sort().join('') === [...t].sort().join('')
};

/**
 * 1. 两数之和
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
    let map = new Map();
    for (let i = 0, len = nums.length; i < len; i++) {
        if (map.has(target - nums[i])) {
            return [map.get(target - nums[i]), i];
        } else {
            map.set(nums[i], i);
        }
    }
    return [];
};

/**
 * 49. 丑数
 * @param {number} n
 * @return {number}
 */
var nthUglyNumber = function (n) {
    const res = new Array(n);
    res[0] = 1;

    let ptr2 = 0, // 下个数字永远 * 2
        ptr3 = 0, // 下个数字永远 * 3
        ptr5 = 0; // 下个数字永远 * 5

    for (let i = 1; i < n; ++i) {
        res[i] = Math.min(res[ptr2] * 2, res[ptr3] * 3, res[ptr5] * 5);
        if (res[i] === res[ptr2] * 2) {
            ++ptr2;
        }
        if (res[i] === res[ptr3] * 3) {
            ++ptr3;
        }
        if (res[i] === res[ptr5] * 5) {
            ++ptr5;
        }
    }

    return res[n - 1];
};
